﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace SDFL.Core.DataStructure
{
    public class Stack<T>
    {
        readonly int m_Size;
        int m_StackPointer = 0;
        T[] m_Items;

        public int Pointer
        {
            get
            {
                return m_StackPointer;
            }
        }

        public Stack(): this(256)
        { 
        }

        public Stack(int size)
        {
            m_Size = size;
            m_Items = new T[m_Size];
        }

        public void Push(T item)
        {
            if (m_StackPointer >= m_Size)
                throw new StackOverflowException();
            m_Items[m_StackPointer] = item;
            m_StackPointer++;
        }

        public void Push(Stack<T> aimStack, params T[] items)
        {
            foreach (T item in items)
            {
                aimStack.Push(item);
            }
        }

        public T Pop()
        {
            m_StackPointer--;
            if (m_StackPointer >= 0)
            {
                return m_Items[m_StackPointer];
            }
            else
            {
                m_StackPointer = 0;
                //throw new InvalidOperationException("Cannot pop an empty stack");
                
                // or you can return default value instead of throwing exception
                return default(T);

            }
        }


    }
}
